<template>
  <div>
    <el-container>
      <!--    侧边栏  -->
      <el-aside :width="asideWidth" style="min-height: 100vh; background-color: #0a3117">
        <div style="height: 60px; color: white; display: flex; align-items: center; justify-content: center">
<!--          <img src="@/assets/logo1.png" alt="" style="width: 40px; height: 40px">-->
          <span class="logo-title" v-show="!isCollapse">灌溉管理系统</span>
        </div>

        <el-menu :default-openeds="['info']" :collapse="isCollapse" :collapse-transition="false" router background-color="#0a3117" text-color="white"
                 active-text-color="#fff" style="border: none" :default-active="$route.path">
          <el-menu-item index="/home" >
            <i class="el-icon-house"></i>
            <span slot="title">首页</span>
          </el-menu-item>

          <el-submenu index="/1" v-if="user.role ==='管理员' && '部门领导'">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>系统管理</span>
            </template>
              <el-menu-item index="/userinfo">用户信息</el-menu-item>
              <el-menu-item index="/notice">系统公告</el-menu-item>
            <el-menu-item index="/logs">系统日志</el-menu-item>
          </el-submenu>

          <el-submenu index="/2">
            <template slot="title">
              <i class="el-icon-eleme"></i>
              <span>灌溉信息管理</span>
            </template>
              <el-menu-item index="/realtime">灌溉实时数据</el-menu-item>
              <el-menu-item index="/standard">灌溉标准数据</el-menu-item>
              <el-menu-item index="/farmland">农田管理</el-menu-item>
          </el-submenu>

            <el-menu-item index="/assign">数据统计分析</el-menu-item>
            <el-menu-item index="/problem">设备异常详情</el-menu-item>
        </el-menu>

      </el-aside>

      <el-container>
        <!--        头部区域-->
        <el-header>

          <i :class="collapseIcon" style="font-size: 26px" @click="handleCollapse"></i>
          <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-left: 20px">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item :to="{ path:$route.path}">{{ $route.meta.name }}</el-breadcrumb-item>
          </el-breadcrumb>

          <div style="flex: 1; width: 0; display: flex; align-items: center; justify-content: flex-end">
            <div style="display: flex">
              <div style="padding-right:16px ">
                <el-tooltip content="系统公告" placement="top">
                  <el-button  @click="drawer = true" type="info" circle style="margin-left: 16px;">
                    <i class="el-icon-s-management"></i>
                  </el-button>
                </el-tooltip>
                <el-drawer
                    title="我是标题"
                    :visible.sync="drawer"
                    :with-header="false"
                    :before-close="handleClose">
                  <el-card style="width: 100%">
                    <div style="margin-bottom: 15px; font-size: 20px; font-weight: bold">系统公告</div>
                    <el-collapse v-model="activeName" accordion>
                      <el-collapse-item  v-for="(item, index) in notices" :key="item.id" :name="index + ''">
                        <template slot="title">
                          <h4>{{ item.title }} </h4> <span style="padding-left: 10px"> {{ item.date }}</span>
                        </template>
                        <div>{{ item.content }}</div>
                      </el-collapse-item>
                    </el-collapse>
                  </el-card>
                </el-drawer>
              </div>
              </div>
            <i class="el-icon-full-screen" style="font-size: 26px" @click="handleFull"></i>
            <el-dropdown placement="bottom">
              <div style="display: flex; align-items: center; cursor: default">
                <img :src="user.avatar ||'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" alt="" style="width: 50px; height: 50px; margin: 0 5px;border-radius: 50%">
                <span>{{ user.name }}</span>
              </div>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="$router.push('/person')">个人信息</el-dropdown-item>
                <el-dropdown-item @click.native="$router.push('/password')">修改密码</el-dropdown-item>
                <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>

        <!--      主体区域-->
        <el-main>

          <router-view  @update:user="updateUser"/>

        </el-main>


      </el-container>


    </el-container>
  </div>
</template>

<script>


import request from "@/utils/request";
import E from "wangeditor";
import hljs from "highlight.js";

export default {
  name: 'Main',
  data() {
    return {
      isCollapse: false,  // 不收缩
      asideWidth: '200px',
      collapseIcon: 'el-icon-s-fold',
      users:[],
      user: JSON.parse(localStorage.getItem('honey-user') || '{}'),
      url:'' ,
      urls:[],
      drawer: false,
      notices: [],
      activeName: '0',
      content: '',
      fromVisible: false,
      form: {},
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
        ]
      },
    }
  },
  mounted: function () {
    // if (!this.user.id){  //当前的浏览器没有用户信息
    //   this.$router.push('/login')
    // }
    this.loadNotice();
    request.get('/user/selectAll').then(res=>{
      this.users=res.data
    })
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
    },
    loadNotice() {
      this.$request.get('/notice/selectUserData').then(res => {
        this.notices = res.data
      })
    },
    updateUser(user){    //获取子组件传过来的数据   更新当前页面的数据
      this.user = JSON.parse(JSON.stringify(user))  //让父级的对象跟子级的对象毫无关联
    },
    logout(){
      localStorage.removeItem('honey-user')  //清楚当前的token和用户数据
      this.$router.push('/login')
    },
    handleFull() {
      document.documentElement.requestFullscreen()
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse
      this.asideWidth = this.isCollapse ? '64px' : '200px'
      this.collapseIcon = this.isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'
    }
  }
}
</script>

<style>
.el-menu--inline {
  background-color: #000c17 !important;
}
.el-menu--inline .el-menu-item {
  background-color: #000c17 !important;
  padding-left: 49px !important;
}
.el-menu-item:hover, .el-submenu__title:hover {
  color: #fff !important;
}
.el-submenu__title:hover i {
  color: #fff !important;
}
.el-menu-item:hover i {
  color: #fff !important;
}
.el-menu-item.is-active {
  background-color: #83d9ab !important;
  border-radius: 5px !important;
  width: calc(100% - 8px);
  margin-left: 4px;
}
.el-menu-item.is-active i, .el-menu-item.is-active .el-tooltip{
  margin-left: -4px;
}
.el-menu-item {
  height: 40px !important;
  line-height: 40px !important;
}
.el-submenu__title {
  height: 40px !important;
  line-height: 40px !important;
}
.el-submenu .el-menu-item {
  min-width: 0 !important;
}
.el-menu--inline .el-menu-item.is-active {
  padding-left: 45px !important;
}


.el-aside {
  transition: width .3s;
  box-shadow: 2px 0 6px rgba(0,21,41,.35);
}
.logo-title {
  margin-left: 5px;
  font-size: 20px;
  transition: all .3s;   /* 0.3s */
}
.el-header {
  box-shadow: 2px 0 6px rgba(0,21,41,.35);
  display: flex;
  align-items: center;
}
</style>